import requests
from pyquery import PyQuery
import os
import datetime
from redis import StrictRedis
import json
import time
import schedule


class Data(object):
    def __init__(self):
        self.title = ''
        self.url = ''
        self.hot = 0


def log(*args, **kwargs):
    format = '%B %d %Y %H:%M:%S'
    value = time.localtime(int(time.time()))
    dt = time.strftime(format, value)
    with open('weibo_log.txt', 'a', encoding='utf-8') as f:
        print(dt, *args, file=f, **kwargs)


def save_body():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
        'Cookie': "SINAGLOBAL=9181227357007.242.1585566008083; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WhXX2wlePFD_8EaOzs2SxWd5JpX5KMhUgL.Foq7ehqceK-ce0z2dJLoI77LxK-LBKBLBKLAUGQt; ALF=1670136260; SSOLoginState=1638600261; SCF=Agt0u5GcvMK9KgMlq0fKkgGGBVudNalLwtLn9qbuR20PXpSBUUnbBCktqlX6eKsrx-0iXSeaH3dMq_2zRdgP0WM.; SUB=_2A25Mr34VDeRhGeBO61QX8SvKyD6IHXVv3ejdrDV8PUNbmtAKLRPYkW9NSlzAlI0N1DRfHlvMonPAAtwqcryzvkFR; _s_tentry=login.sina.com.cn; UOR=,,login.sina.com.cn; Apache=745628668935.1499.1638600261792; ULV=1638600261797:20:1:1:745628668935.1499.1638600261792:1635396295786;"
    }
    response = requests.get('https://s.weibo.com/top/summary?Refer=top_hot&topnav=1&wvr=6',headers=headers)
    body = response.content.decode('utf-8',"ignore")
    path = os.path.join('weibo', '{}.html'.format(datetime.date.today()))
    if not os.path.exists('weibo'):
        os.mkdir('weibo')
    with open(path, 'w', encoding='utf-8') as f:
        f.write(body)
    return body


def get_data_list(body):
    hot_list = PyQuery(body)('tbody')('tr')
    data_list = []
    for e in hot_list:
        data = Data()
        data.title = PyQuery(e)('.td-02')('a').text()
        data.url = 'https://s.weibo.com' + PyQuery(e)('.td-02')('a').attr('href')
        data.hot = PyQuery(e)('.td-02')('span').text()
        data_list.append(data)
    log("示例：第二条", data_list[1].__dict__)
    return data_list


def save_redis(redis, data_list):
    redis.delete('weibo_hot')
    for o in data_list:
        # s = json.dumps(o.__dict__, ensure_ascii=False)
        s = json.dumps(o.__dict__, ensure_ascii=False)
        redis.rpush('weibo_hot', s)


def run():
    body = save_body()
    data_list = get_data_list(body)
    redis = StrictRedis(host='localhost', port=6379, decode_responses=True)
    save_redis(redis, data_list)
    print(redis.lrange('weibo_hot', 0, 10))
    log('微博热搜已经存入reis，List的key是weibo_hot')


if __name__ == '__main__':
    run()
    # schedule.every().day.at('07:00').do(run)
    # while True:
    #     schedule.run_pending()
